﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.Category.controls
{
    public partial class insert : UserControl
    {
        public insert()
        {
            InitializeComponent();
            this.Loaded += (obj, e) =>
            {
                OnLoad();
            };
        }

        public JuCloudy.Data.Entity.SysCategory Entity { get; set; }

        public JuCloudy.Sys.App.Category.MainPage Mainpage { get; set; }

        public ObservableCollection<JuCloudy.Data.Entity.SysStructure> SysStructures { get; set; }

        public void OnLoad()
        {
            foreach (var m in SysStructures)
            {
                this.select_Structure.Items.Add(new ComboBoxItem()
                {
                     Tag=m.StructureId, Content=m.StructureHeader 
                });
            }

            if (Entity != null)
            {
                this.txt_introduction.Text = Entity.CategoryIntruduction;
                this.txt_name.Text = Entity.CategoryName;
                this.txt_order.Text = Entity.CategoryOrder.ToString();
                this.chk_visable.IsChecked = Entity.IsVisable;
            }

            this.btn_submit.Click += (obj, e) =>
            {
                if(String.IsNullOrEmpty(this.txt_introduction.Text)||
                    String.IsNullOrEmpty(this.txt_name.Text)||
                    String.IsNullOrEmpty(this.txt_order.Text)||
                    this.chk_visable.IsChecked==null||
                    this.select_Structure.SelectedItem==null)
                {
                    throw new Exception("表单不能为空！");
                }
                else{
                    try{
                        var test=int.Parse(this.txt_order.Text);
                    }
                    catch{
                        throw new Exception("排序只能为数字");
                    }
                }

                var model = new Data.Entity.SysCategory()
                {
                    CategoryIntruduction = this.txt_introduction.Text,
                    CategoryName = this.txt_name.Text,
                    CategoryOrder = int.Parse(this.txt_order.Text),
                    CreateDate = DateTime.Now,
                    IsVisable = (bool)this.chk_visable.IsChecked,
                    StructureId = (int)((ComboBoxItem)this.select_Structure.SelectedItem).Tag
                };
                if (this.Entity != null)
                    model.CategoryId = Entity.CategoryId;

                Mainpage.insert(model);
            };
        }
    }
}
